﻿namespace WPF.TestEditor.Controls.History
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Threading;
    using ICSharpCode.TreeView;
    using System.Runtime.InteropServices.ComTypes;
    using System.Windows.Controls;
    using System.Windows;
    using System.Windows.Input;
    using EasyStep.Business.Constants;
    using EasyStep.TestExecution;
    using EasyStep.TestExecution.Data;

    public class HistoryItemInfo : SharpTreeNode
    {

        public object Tag { get; set; }
        public Action<HistoryItemInfo> OnCollapsingAction;
        public Action<HistoryItemInfo> OnExpandingAction;
        public Action<HistoryItemInfo> OnChildrenAsyncChaged;
        private HistoryView parentView;

        public override void RaisePropertyChanged(string name)
        {
            base.RaisePropertyChanged(name);
        }

        public HistoryItemInfo(HistoryView view, string text, string iconFullPath, HistoryItemInfo defaultChildItem = null)
        {
            parentView = view;
            this.Title = text;
            icon = new Uri(iconFullPath);
            if (defaultChildItem != null) Children.Add(defaultChildItem);
            LazyLoading = false;
        }
        public HistoryItemInfo(HistoryView view, string text, string iconFullPath, IEnumerable<HistoryItemInfo> children)
        {
            parentView = view;
            this.Title = text;
            icon = new Uri(iconFullPath);
            if (children != null)
                Children.AddRange(children);
            LazyLoading = false;
        }
        protected override void OnCollapsing()
        {
            if (OnCollapsingAction != null)
                OnCollapsingAction(this);
        }
        protected override void OnExpanding()
        {
            if (OnExpandingAction != null)
                OnExpandingAction(this);
        }

        public string Title { get; set; }
        public override object Text
        {
            get
            {
                return Title;
            }
        }
        public string ToolTipText { get; set; }
        public override object ToolTip
        {
            get
            {
                return ToolTipText;
            }
        }
        public override string ToString()
        {
            return Title;
        }


        public override bool CanDrag(SharpTreeNode[] nodes)
        {
            return false;
        }



        public void Open()
        {
            var current = this;
            var projNode = parentView.GetParentNode(current, 1);
            var testNode = projNode == null ? null : parentView.GetParentNode(current, 2);
            var sessionNode = testNode == null ? null : parentView.GetParentNode(current, 3);
            var browserNode = sessionNode == null ? null : parentView.GetParentNode(current, 4);
            var path = parentView.TestDataContext.PathReplacements[MainApplicationServiceConstants.FileSystemDataStoragePath];
            if (projNode != null) path = System.IO.Path.Combine(path, projNode.Title);
            if (testNode != null) path = System.IO.Path.Combine(path, testNode.Title);
            if (sessionNode != null) path = System.IO.Path.Combine(path, sessionNode.Title);


            using (var p = System.Diagnostics.Process.Start(path))
            {
                if (p != null)
                    p.Close();
            }
        }
        public override void DeleteCore()
        {
            var node = this;
            var projectNode = parentView.GetParentNode(node, 1);
            var testNode = parentView.GetParentNode(node, 2);
            var sessionNode = parentView.GetParentNode(node, 3);
            var browserNode = parentView.GetParentNode(node, 4);
            var path = parentView.TestDataContext.PathReplacements[MainApplicationServiceConstants.FileSystemDataStoragePath];
            if (projectNode != null) path = System.IO.Path.Combine(path, projectNode.Title);
            if (testNode != null) path = System.IO.Path.Combine(path, testNode.Title);
            else
            {
                ConfirmDelete("All Test for project", projectNode, () => System.IO.Directory.Delete(path, true));
                return;
            }
            if (sessionNode != null) path = System.IO.Path.Combine(path, sessionNode.Title);
            else
            {
                ConfirmDelete("All Test Results", testNode, () => System.IO.Directory.Delete(path, true));
                return;
            }
            ConfirmDelete("Results", sessionNode, () => System.IO.Directory.Delete(path, true));

        }
        private void ConfirmDelete(string deleteType, HistoryItemInfo node, Action onConfirmed)
        {
            var parent = node;
            var nodesPath = "";
            while (parent != null)
            {
                nodesPath = string.Format("[{0}] > {1}", parent.Text, nodesPath);
                parent = parent.Parent as HistoryItemInfo;
            }
            try
            {
                onConfirmed();
                if (node != null && node.Parent != null)
                {
                    parent = node.Parent as HistoryItemInfo;
                    parent.IsSelected = true;
                    parent.IsExpanded = true;
                    parent.IsSelected = true;
                }
            }
            catch (Exception ex)
            {
                parentView.TestDataContext.Log.Fatal(ex);
                MessageBox.Show("Can't Delete " + nodesPath + "\r\n\r\n" + ex.Message + "\r\n", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }


        private Uri icon;

        public override object Icon
        {
            get
            {
                return icon;
            }
        }

        public override object ExpandedIcon
        {
            get
            {
                return icon;
            }
        }


        public TestStepResult StepResult { get; set; }
    }
}

